﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using iBook2.Models;

namespace iBook2.Controllers
{
    public class PublisherController : ApiController
    {
        private iBookEntities db = new iBookEntities();

        // GET api/Publisher
        public IEnumerable<NhaXuatBan> GetNhaXuatBans()
        {
            return db.NhaXuatBans.AsEnumerable();
        }

        // GET api/Publisher/5
        public NhaXuatBan GetNhaXuatBan(string id)
        {
            NhaXuatBan nhaxuatban = db.NhaXuatBans.Find(id);
            if (nhaxuatban == null)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
            }

            return nhaxuatban;
        }

        // PUT api/Publisher/5
        public HttpResponseMessage PutNhaXuatBan(string id, NhaXuatBan nhaxuatban)
        {
            if (ModelState.IsValid && id == nhaxuatban.MaNhaXuatBan)
            {
                db.Entry(nhaxuatban).State = EntityState.Modified;

                try
                {
                    db.SaveChanges();
                }
                catch (DbUpdateConcurrencyException)
                {
                    return Request.CreateResponse(HttpStatusCode.NotFound);
                }

                return Request.CreateResponse(HttpStatusCode.OK);
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }

        // POST api/Publisher
        public HttpResponseMessage PostNhaXuatBan(NhaXuatBan nhaxuatban)
        {
            if (ModelState.IsValid)
            {
                db.NhaXuatBans.Add(nhaxuatban);
                db.SaveChanges();

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, nhaxuatban);
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = nhaxuatban.MaNhaXuatBan }));
                return response;
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }

        // DELETE api/Publisher/5
        public HttpResponseMessage DeleteNhaXuatBan(string id)
        {
            NhaXuatBan nhaxuatban = db.NhaXuatBans.Find(id);
            if (nhaxuatban == null)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }

            db.NhaXuatBans.Remove(nhaxuatban);

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }

            return Request.CreateResponse(HttpStatusCode.OK, nhaxuatban);
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}